var fs = require("fs");
var path = require("path");
var config = require("./config");
var base = require("./base.config.js");
var merge = require("webpack-merge");
var HtmlWebpackPlugin = require("html-webpack-plugin");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var webpack = require("webpack");

// function getDirectories(src) {
//     return fs.readdirSync(src).filter((file) => {
//         return fs.statSync(path.join(src, file)).isDirectory();
//     });
// }

// var components = getDirectories(path.resolve(config.PATH_SOURCE, "components"));

// components.forEach((component) => {
//     base.entry[path.join('components', component, 'index')] = ['./source/components/' + component + "/index.js"];
// });

// Enable the "stand alone components import"

module.exports = merge(base, {
    output: {
        publicPath: '',
        library: 'VueMirror',
        libraryTarget: 'umd'
    },
    externals: {
        vue: {
            commonjs: 'vue',
            commonjs2: 'vue',
            amd: 'vue',
            root: 'Vue',
            var: 'Vue'
        }
        // Set Vue externals so that it can exclude from the bundle.
    },
    module: {
        rules: [{
                test: /\.vue$/,
                loader: "vue-loader",
                exclude: [
                    config.PATH_DOCUMENT
                    // exclude the document's page
                ],
                options: {
                    loaders: {
                        css: ExtractTextPlugin.extract({
                            use: 'css-loader!postcss-loader!less-loader',
                            fallback: 'vue-style-loader'
                        }),
                        less: ExtractTextPlugin.extract({
                            use: 'css-loader!postcss-loader!less-loader',
                            fallback: 'vue-style-loader'
                        }),
                    }
                }
            },
            {
                test: /\.vue$/,
                loader: "vue-loader",
                include: [
                    config.PATH_DOCUMENT
                    // include the document's page
                ],
                options: {
                    loaders: {
                        css: "vue-style-loader!css-loader",
                        less: "vue-style-loader!css-loader!less-loader",
                    }
                }
            },
            {
                test: /\.css$/,
                loader: ExtractTextPlugin.extract({
                    use: 'css-loader',
                    fallback: 'vue-style-loader'
                })
            },
            {
                test: /\.less$/,
                loader: ExtractTextPlugin.extract({
                    use: 'css-loader!less-loader',
                    fallback: 'vue-style-loader'
                })
            }
        ]
    },
    plugins: [
        new webpack.LoaderOptionsPlugin({
            minimize: true,
            debug: false,
            options: {
                postcss: function() {
                    return [
                        require("autoprefixer")({
                            browsers: ['last 3 versions', 'not IE < 10']
                        })
                    ]
                }
            }
        }),
        new ExtractTextPlugin({
            filename: "[name].css"
        }),
        new HtmlWebpackPlugin({
            template: path.resolve(config.PATH_DOCUMENT, "index.html"),
            filename: path.resolve(config.PATH_BUILD, "document.html"),
            inject: true
        }),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            },
            output: {
                comments: false
            },
            sourceMap: false
        })
    ]
});